/**
 * 权限相关的工具类
 */
layui.define(['jquery','tool'],function (exports) {

    var $ = layui.jquery;
    var tool = layui.tool;

    var obj ={
        /**
         * 动态生成角色选择框
         * @param tragetId 目标框id
         * @param selectOption 默认选中的数据
         * @param firstOption 第一个操作值
         */
        createRoleCodeSelect:function (tragetId,ignoreOptions,selectOption,firstOption) {
            $.ajax({
                url:'/back/role/getAllList'
                ,type:'post'
                ,data:{
                }
                ,async:false
                ,dataType:'json'
                ,success:function(res){
                    if (res.code === return_200) {
                        var selectHtml = "";
                        if(!tool.isEmpty(firstOption)){
                            selectHtml += "<option value=''>" + firstOption + "</option>";
                        }
                        $("#" + tragetId).empty();
                        for (var i = 0; i < res.data.length; i++) {
                            var flag = false;
                            if (ignoreOptions && ignoreOptions.length != 0) {
                                for (var j = 0; j < ignoreOptions.length; j++) {
                                    if (res.data[i].roleCode == ignoreOptions[j]) {
                                        flag = true;
                                        break;
                                    }
                                }
                            }
                            if (flag) {
                                continue;
                            }
                            if (selectOption && res.data[i].roleCode == selectOption) {
                                selectHtml += "<option value=\"" + res.data[i].roleCode + "\" selected>" + res.data[i].roleName + "</option>";
                            } else {
                                selectHtml += "<option value=\"" + res.data[i].roleCode + "\">" + res.data[i].roleName + "</option>";
                            }
                        }
                        $("#" + tragetId).append(selectHtml);
                    } else {
                        console.log("初始化选择框失败");
                    }
                }
                ,error:function(){
                    console.log("初始化选择框异常");
                }
            });
        }
        ,getRoleName:function(roleCode){
            var roleName = "";
            $.ajax({
                url:'/back/role/getAllList'
                ,type:'post'
                ,data:{
                    roleCode:roleCode
                }
                ,async:false
                ,dataType:'json'
                ,success:function(res){
                    if (res.code === return_200) {
                        if(res.data.length > 0){
                            roleName = res.data[0].roleName;
                        }
                    }
                    return null;
                }
                ,error:function(){
                    console.log("初始化选择框异常");
                }
            });
            return roleName;
        }
    };
    exports("roleUtil",obj);
    }
);